package cn.edu.cdtu.healthserver.dao.impl;

import java.sql.SQLException;
import java.util.List;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import cn.edu.cdtu.healthserver.dao.InternetHealthNewsDao;
import cn.edu.cdtu.healthserver.domain.NewsFromInternet;
import cn.edu.cdtu.healthserver.utils.C3P0Util;

public class InternetHealthNewsDaoImplementation implements InternetHealthNewsDao
{
	QueryRunner queryRunner=new QueryRunner(C3P0Util.getDataSource());
	public boolean addNews(NewsFromInternet news)
	{
		String sql="insert into InternetHealthNews(title,link,description,pubDate)values(?,?,?,?)";
		Object params[]=new Object[]{news.getTitle(),news.getLink(),news.getDescription(),news.getPubDate()};
		try 
		{
			queryRunner.update(sql,params);
			return true;
		} 
		catch (SQLException e) 
		{
			e.printStackTrace();
			return false;
		}
	}

	public List<NewsFromInternet> queryNews() 
	{
		String sql="select * from InternetHealthNews";
		try 
		{
			return queryRunner.query(sql,new BeanListHandler<NewsFromInternet>(NewsFromInternet.class));
		} 
		catch (SQLException e) 
		{
			throw new RuntimeException();
		}
	}
}
